<div class="page-header">
    <h1>
        Daftar Produk
        <small>
            <i class="icon-double-angle-right"></i>
            Daftar Produk

        </small>
    </h1>
</div>

<?php
if ($this->msg) {
    ?>
    <div class="alert alert-info">
        <button type="button" class="close" data-dismiss="alert">
            <i class="icon-remove"></i>
        </button>

        <strong>
            <i class="icon-ok"></i>
        </strong>

        <?php echo $this->msg; ?>
        <br />
    </div>
<?php
}


if ($this->errmsg) {
    ?>
    <div class="alert alert-danger">
        <button type="button" class="close" data-dismiss="alert">
            <i class="icon-remove"></i>
        </button>

        <strong>
            <i class="icon-remove"></i>

        </strong>

        <?php echo $this->errmsg; ?>
        <br />
    </div>
<?php
}
?>

<div class="row">
        <div class="col-xs-11">
            <!-- PAGE CONTENT BEGINS -->
            <form id="frm" class="form-horizontal" role="form" action="" method="post">
                <div class="form-group">
                    <label class="col-sm-2 control-label no-padding-right" for="product_code"> Kode Produk </label>

                    <div class="col-sm-9">
                        <input class="col-xs-10 col-sm-5" type="text" style="width:150px" name="product_code" id="product_code" value="<?php echo (isset($this->data['sn'])) ? $this->data['sn'] : ""; ?>"></input>

                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label no-padding-right" for="suplier_id"> Suplier </label>

                        <div class="col-sm-9">
                        <select class="col-xs-10 col-sm-5 chosen-select" id="suplier_id" name="suplier_id" data-placeholder="Pilih Suplier..." required>
                            <option value=""> Pilih Suplier</option>
                            <?php
                                foreach($this->supliers as $suplier){
                                        echo '<option value="' . $suplier['suplier_id'] . '">' . $suplier['suplier_name']. ' - ' .$suplier['suplier_address'] . '</option>';
                                }

                            ?>
                        </select>
			</div>

                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label no-padding-right" for="start_dat"> Tanggal Harga Berlaku </label>

                    <div class="col-sm-9">
                        <input type="text" class="date-picker" required name="start_dat" id="start_dat" placeholder="Tanggal awal"></input>
                        &nbsp;s/d&nbsp;<input type="text" required class="date-picker" name="end_dat" id="end_dat" placeholder="Tanggal akhir"></input>
                        <button class="btn btn-info btn-sm" type="button" onclick="cari()">
                            Cari
                        </button>
                    </div>
                </div>



        </form>
        <br/>

        <table id="grid-table"></table>

        <div id="grid-pager"></div>

        <script type="text/javascript">
                var $path_base = "<?php echo $this->url()?>";//this will be used in gritter alerts containing images
        </script>


        <!-- PAGE CONTENT ENDS -->
    </div>
</div>
<script>

    var nowTemp = new Date();
    var now = new Date(nowTemp.getFullYear(), nowTemp.getMonth(), nowTemp.getDate(), 0, 0, 0, 0);

    var satrtDate = $('#start_dat').datepicker({
      format: 'yyyy-mm-dd'
    }).on('changeDate', function(ev) {
      //if (ev.date.valueOf() > endDate.date.valueOf()) {
        var newDate = new Date(ev.date)
        newDate.setDate(newDate.getDate() + 1);
        endDate.setValue(newDate);
      //}
      satrtDate.hide();
      $('#end_dat')[0].focus();
    }).data('datepicker');

    var endDate = $('#end_dat').datepicker({
      onRender: function(date) {
        return date.valueOf() <= satrtDate.date.valueOf() ? 'disabled' : '';
      }, format: 'yyyy-mm-dd'
    }).on('changeDate', function(ev) {
      endDate.hide();
    }).data('datepicker');




    jQuery(function($) {

            jQuery('#grid-table').jqGrid({
                url:'<?php echo $this->url() ?>/../../jsondata/productlist',
                datatype: "json",
                height: 150,
                colNames:['Kode<br>Produk','Nama<br>Produk', 'Deskripsi<br>Produk','Parent<br>Produk','Owner','Suplier','Harga Asal','Harga Jual','Tanggal<br>Harga Berlaku','Jumlah','Aksi'],
                colModel:[
                    {name:'KodeProduk',index:'product_code'},
                    {name:'NamaProduk',index:'product_name'},
                    {name:'DeskProduk',index:'product_desc'},
                    {name:'ParentProd',index:'product_parent'},
                    {name:'Owner',index:'product_owner_name'},
                    {name:'Suplier',index:'suplier_name'},
                    {name:'HargaAsal',index:'basic_price', align:"right", formatter: 'number', hidden:true},
                    {name:'HargaJual',index:'sale_price', align:"right", formatter: 'number', hidden:true},
                    {name:'TglHarga',index:'start_dat', hidden:true},
                    {name:'Jumlah',index:'qty', align:"right", hidden:true},
                    {name:'Aksi',index:'aksi',align:"center", width: 200}
                ],
                rowNum:10,
                rowList:[10,20,30],
                pager: '#grid-pager',
                rownumbers: true,
                viewrecords: true,
                multiboxonly: true,
                caption:"Daftar Produk",

                loadComplete : function() {
                        var table = this;
                        setTimeout(function(){
                                updateActionIcons(table);
                                updatePagerIcons(table);
                                enableTooltips(table);
                        }, 0);
                },

                autowidth: true
            });

            jQuery('#grid-table').jqGrid('navGrid','#grid-pager',{edit:false,add:false,del:false});


            //navButtons
            jQuery('#grid-table').jqGrid('navGrid','#grid-pager',
                    { 	//navbar options
                            search: true,
                            searchicon : 'ui-icon-search orange',
                            refresh: true,
                            refreshicon : 'ui-icon-refresh green'
                    }
            )




            //unlike navButtons icons, action icons in rows seem to be hard-coded
            //you can change them like this in here if you want
            function updateActionIcons(table) {

                    var replacement =
                    {
                            'ui-icon-search' : 'ui-icon-search orange',
                            'ui-icon-refresh' : 'ui-icon-refresh green'
                    };
                    $(table).find('.ui-pg-div span.ui-icon').each(function(){
                            var icon = $(this);
                            var $class = $.trim(icon.attr('class').replace('ui-icon', ''));
                            if($class in replacement) icon.attr('class', 'ui-icon '+replacement[$class]);
                    })

            }

            //replace icons with FontAwesome icons like above
            function updatePagerIcons(table) {
                    var replacement =
                    {
                            'ui-icon-seek-first' : 'icon-double-angle-left bigger-140',
                            'ui-icon-seek-prev' : 'icon-angle-left bigger-140',
                            'ui-icon-seek-next' : 'icon-angle-right bigger-140',
                            'ui-icon-seek-end' : 'icon-double-angle-right bigger-140'
                    };
                    $('.ui-pg-table:not(.navtable) > tbody > tr > .ui-pg-button > .ui-icon').each(function(){
                            var icon = $(this);
                            var $class = $.trim(icon.attr('class').replace('ui-icon', ''));

                            if($class in replacement) icon.attr('class', 'ui-icon '+replacement[$class]);
                    })
            }

            function enableTooltips(table) {
                    $('.navtable .ui-pg-button').tooltip({container:'body'});
                    $(table).find('.ui-pg-div').tooltip({container:'body'});
            }

            //var selr = jQuery(grid_selector).jqGrid('getGridParam','selrow');

    });




    function cari(){
        var prodCode = $('#product_code').val();
        var supId = $('#suplier_id').val();
        var date1 = $('#start_dat').val();
        var date2 = $('#end_dat').val();


        jQuery("#grid-table").jqGrid('setGridParam',{
            url:"<?php echo $this->url() ?>/../../jsondata/productlist?product_code="+prodCode+"&suplier_id="+supId+"&start_dat="+date1+"&end_dat="+date2
        }).trigger("reloadGrid");

    }


    function price(prodId){
        location.href = "<?php echo $this->url() ?>price?prodId="+prodId;
    }

    function stock(prodId){
        location.href = "<?php echo $this->url() ?>stock?prodId="+prodId;
    }

    function edit(prodId){
        location.href = "<?php echo $this->url() ?>edit?prodId="+prodId;
    }

    function del(prodId){
        
        bootbox.confirm("Hapus Produk ?", function(result) {
                if(result) {
                    jQuery("#grid-table").jqGrid('setGridParam',{
                        url:"<?php echo $this->url() ?>/../../jsondata/productlist?act=del&prodId="+prodId
                    }).trigger("reloadGrid");
                }
        });
    }


</script>




